﻿/*
 
Celerity: Sensory Overload
Copright 2012 by Adam Hill, David Gannon, Thomas Tamblyn & Patrick Ytting
Contact: adam.hill@outlook.com
Website: adamhill.org
Licence: MS-Pl Licence
  
 */

using Celerity.Data;
using Celerity.Util;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Text;

namespace Celerity.UI
{
    public partial class UILayer
    {
        public void Update(int elapsedMilliseconds, IEnumerable<Vector2> interactions, MouseState mouse, int framesCaptured, float faceCentrePercentX, float faceCentrePercentY, float faceSizePercentWidth, float faceSizePercentHeight)
        {
            updateCounter++;
            this.mouse.X = mouse.X;
            this.mouse.Y = mouse.Y;
            controls.Update(interactions);
            UpdateFPS(elapsedMilliseconds, framesCaptured);
            UpdateWebcamCalibrator(faceCentrePercentX, faceCentrePercentY, faceSizePercentWidth, faceSizePercentHeight);
        }

        void UpdateFPS(int elapsedMilliseconds, int framesCaptured)
        {
            StatisticsUtil.Update(elapsedMilliseconds);
            fpsTextPos = new Vector2(4, CeleritySettings.WindowHeight - 32);
            string gap = "   ";
            StringBuilder sb = new StringBuilder();
            sb.Append(Math.Round(StatisticsUtil.FPS, 2).ToString() + "fps" + gap);
            sb.Append(Math.Round(StatisticsUtil.FPS, 2).ToString() + "dps" + gap);
            sb.Append(framesCaptured + " samples" + gap);
            sb.Append(updateCounter + " updates" + gap);
            sb.Append(drawCounter + " draws" + gap);
            sb.Append(GlobalGameStates.GameState.ToString() + gap);
            sb.Append(GlobalGameStates.MenuState.ToString() + gap);
            sb.Append(GlobalGameStates.MuteState.ToString() + gap);
            fpsText = sb.ToString();
        }

        void UpdateWebcamCalibrator(float faceCentrePercentX, float faceCentrePercentY, float faceSizePercentWidth, float faceSizePercentHeight)
        {
            if (GlobalGameStates.MenuState != MenuState.MainMenu)
            {
                return;
            }

            float boxW = 300; // Actual width of rectangle, not bounds of dotted line
            float boxH = boxW;
            float boxX = (CeleritySettings.WindowWidth / 2f - boxW / 2f);
            float boxY = (CeleritySettings.WindowHeight / 2f - boxH / 2f);
            float faceBlobWidth = (faceSizePercentWidth * boxW);
            float faceBlobHeight = (faceSizePercentHeight * boxH);
            float faceBlobX = boxX + ((faceCentrePercentX) * boxW - faceBlobWidth / 2f);
            float faceBlobY = boxY + (faceCentrePercentY * boxH - faceBlobHeight / 2f);
            calibratorFaceBlob = new Rectangle((int)faceBlobX, (int)faceBlobY, (int)faceBlobWidth, (int)faceBlobHeight);
        }
    }
}
